package com.qf.dao;

import com.qf.pojo.User;
import com.qf.utils.C3P0Utils;
import com.qf.utils.DBUtil;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Date;

/**
 * @author 86199
 * @version V1.0
 * @Project jqueryDemo
 * @Package com.qf.dao
 * @Description:
 * @Date 2022/5/21 16:52
 */
public class UserDaoImpl  implements UserDao{

    //获取数据源
    //    创建dbutils工具
    private QueryRunner queryRunner = new QueryRunner(DBUtil.getDateSource());
    @Override
    public User findByUserName(String userName){

        String sql = "select * from tb_user where username=?";
        try {
            User user = queryRunner.query(sql, new BeanHandler<>(User.class), userName);
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  null;
        }

    @Override
    public int addUser(User user) {
        String sql = "insert into tb_user (username, password, email, gender, status, role, code) value(?, ?, ?, ?, ?, ?, ?)";
        try {
            int count = queryRunner.update(sql, user.getUsername(), user.getPassword(), user.getEmail(),
                    user.getGender(), user.getStatus(), user.getRole(), user.getCode());
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

}

